﻿package com.wandian.view.components.ui
{
	import com.wandian.data.GameData;
	import com.wandian.model.vo.CardVo;

	/**   
	* @author long
	* @QQ 108232706
	* @version 1.0   
	*/   
	public class CardCount extends Object
	{
		private static var cardCount:CardCount;

		public function CardCount():void
		{
			return;
		}

		public function countCardHuaSe(huase:String):int
		{
			var frame:int=0;
			switch (huase)
			{
				case "d": //方块
				{
					frame=1;
					break;
				}
				case "c": //梅花
				{
					frame=2;
					break;
				}
				case "h": //红桃
				{
					frame=3;
					break;
				}
				case "s": //黑桃
				{
					frame=4;
					break;
				}
				default:
				{
					break;
				}
			}
			return frame;
		}

		public function countCardNum(cardvalue:String):int
		{
			var frame:int=0;
			if (cardvalue == "A")
			{
				frame=1;
			}
			else if (cardvalue == "T")
			{
				frame=10;
			}
			else if (cardvalue == "J")
			{
				frame=11;
			}
			else if (cardvalue == "Q")
			{
				frame=12;
			}
			else if (cardvalue == "K")
			{
				frame=13;
			}
			else
			{
				frame=int(cardvalue);
			}
			return frame;
		}

		/**
		 * 计算牌类型
		 * @param arrCards
		 * @return
		 */
		public function countCardType(arrCards:Array):Object
		{
			var cardTypeData2:Object=null;
			var cardStr:String=null;
			var cardVo:CardVo=null;
			var shunziData:Object=null;
			var _loc_2:String="";
			var arrHuase:Array=[[], [], [], []];
			var arrCardVo:Array=[];
			var total:*=arrCards.length;
			var index:int=0;
			while (index < total)
			{

				if (arrCards[index] == "-")
				{
				}
				else
				{
					cardStr=arrCards[index];
					cardVo=new CardVo();
					cardVo.num=this.countCardNum(cardStr.substring(0, 1));
					cardVo.huase=this.countCardHuaSe(cardStr.substring(1, 2));
					arrHuase[(cardVo.huase - 1)].push(cardVo);
					arrCardVo.push(cardVo);
					arrHuase[(cardVo.huase - 1)]=GameData.sortMaxToMinArray(arrHuase[(cardVo.huase - 1)], "num");
				}
				index=index + 1;
			}
			arrCardVo=GameData.sortMaxToMinArray(arrCardVo, "num");
			//--------------同花5
			var cardTypeData:Object=this.getFlush(arrHuase); //{type: 5, arr1: copySameColor, huase: huaseIndex:1-4};
			//高牌0|一对1|两对2|三条3|顺子4|同花5|葫芦6|四条7|同花顺8|皇家同花顺9

			//---------顺子4,同花顺8|皇家同花顺9
			if (typeof(cardTypeData) != "string")
			{
				cardTypeData2=cardTypeData;
				cardTypeData=this.getStraight(cardTypeData.arr1, cardTypeData);
				if (typeof(cardTypeData) != "string")
				{
					return cardTypeData;
				}
			}
			//--------------四条7
			cardTypeData=this.getFourAkind(arrCardVo);
			if (typeof(cardTypeData) != "string")
			{
				return cardTypeData;
			}
			//葫芦6,三条3,两对2,一对1,高牌0
			cardTypeData=this.getFullHouse(arrCardVo);
			if (cardTypeData.type == 6)
			{
				return cardTypeData;
			}
			if (cardTypeData2 == null)
			{
				//顺子
				shunziData=this.getStraight(arrCardVo);
				if (typeof(shunziData) != "string")
				{
					return shunziData;
				}
				return cardTypeData;
			}
			else
			{
				return cardTypeData2;
			}
		}

		/**
		 * 	//高牌0|一对1|两对2|三条3|顺子4|同花5|葫芦6|四条7|同花顺8|皇家同花顺9
		 *<br/>皇家同花顺9,同花顺8,顺子4
		 * @param arrSomecolor {"1h","2h","3h","4h","5h"}
		 * @param sameColorData {type: 5, arr1: arrSomecolor, huase: huaseIndex:1-4};
		 * @return
		 */
		private function getStraight(arrSomecolor:Array, sameColorData:Object=null):Object
		{
			var cardVo:CardVo=null;
			var cardVo2:CardVo=null;
			var tempCardVo:CardVo=null;
//			var _loc_3:Array=[];
//			var _loc_4:int=0;
//			var arrSomecolor:Array=[];
//			arrSomecolor=arrSomecolor;
			var sameLen:int=arrSomecolor.length;
			if (arrSomecolor[(sameLen - 1)]["num"] == 1)
			{
				cardVo=new CardVo();
				cardVo.num=14;
				cardVo.huase=arrSomecolor[(sameLen - 1)]["huase"];
				arrSomecolor.push(cardVo);
				arrSomecolor=GameData.sortMaxToMinArray(arrSomecolor, "num");
			}
			sameLen=arrSomecolor.length;
			var arrContinueCard:Array=[];
			var index:int=0;
			while (index < sameLen)
			{

				cardVo2=arrSomecolor[index];
				if (index == 0)
				{
					arrContinueCard.push(cardVo2);
				}
				else
				{
					//[5,4,4,3,2,1]
					tempCardVo=arrSomecolor[(index - 1)] as CardVo;
					//后面的+1=前面的
					if ((cardVo2.num + 1) == tempCardVo.num)
					{
						arrContinueCard.push(cardVo2);
					}
					//如果值相等，继续
					else if (cardVo2.num == tempCardVo.num)
					{
						index++;
						continue;
					}
					//arrContinueCard[5,4,3,2,1]
					else if (arrContinueCard.length >= 5)
					{
						break;
					}
					//前面的不符合，后面开始重新计算
					else
					{
						arrContinueCard=[];
						arrContinueCard.push(cardVo2);
					}
				}
				index++;
			}
			if (arrContinueCard.length >= 5)
			{
				if (sameColorData != null)
				{
					//14是临时增加的
					if (arrContinueCard[0]["num"] == 14)
					{
						arrContinueCard.pop();
						//皇家同花顺
						return {type: 9, arr1: arrContinueCard, huase: sameColorData.huase};
					}
					//同花顺
					return {type: 8, arr1: arrContinueCard, huase: sameColorData.huase};
				}
				else
				{
					//顺子
					return {type: 4, arr: arrContinueCard};
				}
			}
			return "0";
		}

		/**
		 *同花
		 * @param arrHuase [[{"1h","2h","3h","4h","5h"}],[],[],[]]
		 * @return
		 */
		private function getFlush(arrHuase:Array):Object
		{
			var huaseLen:int=0;
			var copySameColor:Array=[];
			var huaseIndex:int=0;
			var index:int=0;
			while (index < arrHuase.length)
			{

				huaseLen=arrHuase[index].length;
				if (huaseLen >= 5)
				{
					huaseIndex=index + 1;
					copySameColor=arrHuase[index].slice(huaseLen - 5, huaseLen);
					break;
				}
				index=index + 1;
			}
			if (huaseIndex == 0)
			{
				return "0";
			}
			return {type: 5, arr1: copySameColor, huase: huaseIndex};
		}

		/**
		 * 葫芦6,三条3,两对2,一对1,高牌0
		 * @param arrCardVo 已排序的数组
		 * @return
		 */
		private function getFullHouse(arrCardVo:Array):Object
		{
			var tempCard:CardVo=null;
			var preCard:CardVo=null;
			var arrLength:*=arrCardVo.length;
			var arrSame:Array=[];
			var index:int=0;
			while (index < arrLength)
			{

				//[3,3,3,1,2]
				tempCard=arrCardVo[index] as CardVo;
				if (index == 0)
				{
					arrSame.push([tempCard]); //[[3,3,3],[1],[2]]
				}
				else
				{
					preCard=arrCardVo[(index - 1)] as CardVo;
					if (tempCard.num == preCard.num)
					{
						arrSame[(arrSame.length - 1)].push(tempCard);
					}
					else
					{
						arrSame.push([tempCard]);
					}
				}
				index=index + 1;
			}
			var arrTwo:Array=[];
			var arrThree:Array=[];
			index=0;
			while (index < arrSame.length)
			{

				if (arrSame[index].length == 3)
				{
					arrThree.push(arrSame[index]);
				}
				else if (arrSame[index].length == 2)
				{
					arrTwo.push(arrSame[index]);
				}
				index=index + 1;
			}
			var threeLen:int=arrThree.length;
			var twoLen:int=arrTwo.length;
			if (threeLen >= 1 && twoLen >= 1)
			{
				if (threeLen == 2)
				{
					arrThree[1].pop();
					return {type: 6, arr1: arrThree[0], arr2: arrThree[1]};
				}
				// 葫芦
				return {type: 6, arr1: arrThree[0], arr2: arrTwo[0]};

			}
			else
			{
				if (threeLen == 1) //三条
				{
					return {type: 3, arr1: arrThree[0]};
				}
				if (twoLen >= 2) //两对
				{
					return {type: 2, arr1: arrThree[0], arr2: arrTwo[1]};
				}
				if (twoLen == 1) //一对
				{
					return {type: 1, arr1: arrTwo[0]};
				}
				//高牌
				return {type: 0, arr1: [arrCardVo[0]]};
			}
		}

		/**
		 * 四条7
		 * @param arrCardVo
		 * @return
		 */
		private function getFourAkind(arrCardVo:Array):Object
		{
			var tempCard:CardVo=null;
			var nextCard:CardVo=null;
			var arrSame:Array=[];
			var arrCardlen:*=arrCardVo.length;
			var index:int=0;
			while (index < arrCardlen)
			{

				tempCard=arrCardVo[index];
				if ((index + 1) >= arrCardlen)
				{
					nextCard=arrCardVo[(index - 1)] as CardVo;
					if (tempCard.num == nextCard.num)
					{
						arrSame.push(tempCard);
					}
					break;
				}
				nextCard=arrCardVo[(index + 1)] as CardVo;
				arrSame.push(tempCard);
				if (tempCard.num != nextCard.num)
				{
					if (arrSame.length < 4)
					{
						arrSame=[];
					}
					else
					{
						break;
					}
				}
				index=index + 1;
			}
			if (arrSame.length == 4)
			{
				return {type: 7, arr1: arrSame}; //四条
			}
			return "0";
		}

		private function getStraightFlush(param1:Array):Array
		{
			var _loc_4:int=0;
			var _loc_2:Array=[];
			var _loc_3:int=0;
			while (_loc_3 < param1.length)
			{

				_loc_4=this.countCardNum(param1[_loc_3].substring(0, 1));
				_loc_2.push(_loc_4);
				_loc_3=_loc_3 + 1;
			}
			_loc_2.sort(Array.NUMERIC);
			if (_loc_2[0] == 1)
			{
			}
			if (_loc_2[4] == 13)
			{
				_loc_2.splice(0, 1);
				_loc_2.push(1);
			}
			return _loc_2;
		}

		public static function instance():CardCount
		{
			if (CardCount.cardCount == null)
			{
				CardCount.cardCount=new CardCount;
			}
			return CardCount.cardCount;
		}

	}
}
